package com.pignest.hotel.ui.income;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Member;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.beans.TbGoods;
import com.pignest.hotel.beans.TbLiveGoodsBorrow;
import com.pignest.hotel.definition.GoodsType;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.service.IncomeService;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class InComeWindow extends Window implements EventListener {

	/**
	 * 入账
	 */
	private static final long serialVersionUID = -5789856626869324954L;
	private IncomeService incomeService;
	private MemberService memberService;
	private List<Living> selectLive;
	private Textbox roomName;
	private Listbox selectLivingList;
	private Listbox proType;
	private Listbox subType;
	private Textbox price;
	private Label count;
	private Textbox remark;
	private Label opTip;
	
	private Textbox livingTime;
	private Textbox leaveTime;
	private Textbox available;
	private Textbox integral;
	private Textbox credit;
	
	private Checkbox manyTimesInChb;
	public InComeWindow()
	{
		incomeService =Services.getIncomeService();
		memberService=Services.getMemberService();
		addEventListener("onCreate", this);
	}
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			roomName =(Textbox)getFellow("roomName");
			selectLivingList=(Listbox)getFellow("selectLivingList");
			proType=(Listbox)getFellow("proType");
			subType=(Listbox)getFellow("subType");
			price=(Textbox)getFellow("price");
			count =(Label)getFellow("count");
			remark=(Textbox)getFellow("remark");
			livingTime=(Textbox)getFellow("livingTime");
			leaveTime=(Textbox)getFellow("leaveTime");
			available=(Textbox)getFellow("available");
			integral=(Textbox)getFellow("integral");
			credit=(Textbox)getFellow("credit");
			opTip=(Label)getFellow("opTip");
			manyTimesInChb=(Checkbox)getFellow("manyTimesInChb");
			init();
			selectLivingList.addEventListener(Events.ON_SELECT, new EventListener(){
				   public void onEvent(Event event) {
					   Living l=UIUtils.getSelectedValue(selectLivingList);
					   initTextbox(l);
					   selectFF(proType);
				   }
			   });
			proType.addEventListener(Events.ON_SELECT, new EventListener(){
				public void onEvent(Event event) {
				   String name=(String)UIUtils.getSelectedValue(proType);
					if(name.equals(InComeItemType.WPXF.name()))
					{
						subType.setVisible(true);
					    initSubPro(GoodsType.OnConsume.name());
					 }else if(name.equals(InComeItemType.PCF.name())){
						 subType.setVisible(true);
						 initSubPro(GoodsType.OnBorrow.name());
					 }else if(name.equals(InComeItemType.FF.name())){
						   subType.getItems().clear();
						   subType.setVisible(false);
						   selectFF(proType);
					   }else
					   {
						   subType.getItems().clear();
						   subType.setVisible(false);
						   price.setValue(null);
					   }
				   }
			   });
			subType.addEventListener(Events.ON_SELECT, new EventListener(){
				   public void onEvent(Event event) {
					   String goodId =(String)UIUtils.getSelectedValue(subType);
					   TbGoods g=incomeService.get(TbGoods.class, goodId);
					   Integer iPrice =g.getPrice();
					   String name=(String)UIUtils.getSelectedValue(proType);
					   if(name.equals(InComeItemType.WPXF.name()))
					   {
						   selectOnSubPro(iPrice);
					   }else if(name.equals(InComeItemType.PCF.name())){
						   setBorrowPrice(g);
					   }
					   
				   }
			   });
			
		}
	}
	public void init()
	{
		if(selectLive==null)
		{
			UIUtils.alert("无效的入住!请选择入住记录");
			this.detach();
		}
		selectLivingList.getItems().clear();
		int i=0;
		for(Living l:selectLive)
		{
			
			Listitem listitem = new Listitem();
			listitem.setParent(selectLivingList);
			listitem.setValue(l);
			if(i==0){
				listitem.setSelected(true);
				initTextbox(l);
			}
			listitem.setLabel("["+l.getRoom().getName()+"]"+l.getCustormName());
			i++;
		}
		initProType();
	}
	/**初始化入住文本
	 * */
	public void initTextbox(Living l)
	{
		l=incomeService.get(Living.class,l.getId() );
		int sPayed =incomeService.countAllConsums(l, true, true);//已付
		int sUsed=incomeService.countAllConsumsUsed(l, true,null);//已消费的
		
		roomName.setValue(l.getRoom().getName());
		livingTime.setValue(DateUtil.toString(l.getLiveTime(), DateUtil.HOTEL_DEFAULT_FORMAT));
		leaveTime.setValue(DateUtil.toString(l.getLeaveDate(), DateUtil.HOTEL_DEFAULT_FORMAT));
		available.setValue(Utils.toPrice(l.getStorage()-sUsed+sPayed));
		if(l.getMember()!=null)
		{
			Integer ger =l.getMember().getIntegral();
			Integer cre =l.getMember().getCredit();
			if(ger!=null)
			  integral.setValue(String.valueOf(ger));
			else
				integral.setValue("0");
			if(cre!=null)
				credit.setValue(String.valueOf(cre));
			else
				credit.setValue("0");
		}else
		{
			integral.setValue("0");
			credit.setValue("0");
		}
			
	}
	/**初始化营业项目
	 * 
	 * */
	public void initProType()
	{
		proType.getItems().clear();
		InComeItemType[] inComeItems=InComeItemType.values();
		for(InComeItemType item:inComeItems){
			String name =item.name();
			if(!name.equals(InComeItemType.YJ.name())&&!name.equals(InComeItemType.JSFF.name())&&!name.equals(InComeItemType.CX.name()))
			{
				Listitem listitem = new Listitem();
				listitem.setParent(proType);
				listitem.setValue(item.name());
				listitem.setLabel(item.getName());
				if(item.name().equals(InComeItemType.WPXF.name()))
				{
					listitem.setSelected(true);
					initSubPro(GoodsType.OnConsume.name());
					
				}else if(item.name().equals(InComeItemType.PCF.name())){
					listitem.setSelected(true);
					initSubPro(GoodsType.OnBorrow.name());
					
				}
				count.setValue("1");
			}
		}
	}
	/**初始化消费项目如物品类*/
	public void initSubPro(String type)
	{
		subType.getItems().clear();
		List<TbGoods> goods=incomeService.queryGoods(type);
		int i=0;
		if(goods==null)
			return ;
		for(TbGoods g:goods)
		{
			Listitem listitem = new Listitem();
			listitem.setParent(subType);
			listitem.setValue(g.getId());
			listitem.setLabel(g.getName());
			if(i==0){
				
				listitem.setSelected(true);
				if(GoodsType.OnBorrow.name().equals(type)){
					setBorrowPrice(g);
				}else{
					selectOnSubPro(g.getPrice());
				}
					
					
			}
			i++;
		}
		
	}
	/**选择商品自动*/
	public void selectOnSubPro(int iPrice)
	{
		price.setValue(Utils.toPrice(iPrice));
		count.setValue("1");
		opTip.setValue("");
	}
	/**选择房费**/
	public void selectFF(Listbox proType)
	{
		String name =UIUtils.getSelectedValue(proType);
		if(name.equals(InComeItemType.FF.name()))
		{
			Living selectLiving =(Living)UIUtils.getSelectedValue(selectLivingList);
			if(selectLiving==null){
				UIUtils.alert("无效的入住！");
				return;
			}
			Room r =selectLiving.getRoom();
			if(r==null)
				return;
			int iPrice=0;
			if(selectLiving.getMember()!=null)//是会员
			{
				Member m =selectLiving.getMember();
				iPrice =memberService.getMemberPrice(m.getId(), r.getType().getId());
			}else
			{
				iPrice =r.getType().getPrice();
			}
			price.setValue(Utils.toPrice(iPrice));
			count.setValue("1");
		}else
		{
			price.setValue("0");
			count.setValue("0");
		}
		opTip.setValue("");
	}
	public void setBorrowPrice(TbGoods g)
	{
		Living l =(Living)UIUtils.getSelectedValue(selectLivingList);
		String LivingId =l.getId();
		List<TbLiveGoodsBorrow> list =Services.getOtherBusiService().getGoodBorrow(LivingId, g.getId());
		if(list!=null&&list.size()!=0){
		  count.setValue(String.valueOf(list.size()));
		  price.setValue(Utils.toPrice(g.getPrice()*list.size()));
		  opTip.setValue("共"+list.size()+"借用物品");
		}
		else{
			count.setValue("0");
			price.setValue("0");
			opTip.setValue("没有借用物品!");
		}
	}
    /**入账**/
	public void inCome()
	{
		try{
			if(!Hotel.onDuty()){
				UIUtils.alert("你未值班不能操作此业务!");
				return ;
			}
			Living l =(Living)UIUtils.getSelectedValue(selectLivingList);
			String type =UIUtils.getSelectedValue(proType);
			String sRemark=remark.getValue();
			String name ="";
			//是否消费类 true 则在price加负数反之不加
			boolean isConsume=false;
			List<TbLiveGoodsBorrow> gb =null;
			if(type.equals(InComeItemType.WPXF.name()))
			{
				String sName =UIUtils.getSelectedLable(subType);
				isConsume=true;
				name=sName;
			}else if(type.equals(InComeItemType.PCF.name())){
				String sName =UIUtils.getSelectedLable(subType);
				isConsume=true;
				name=sName;
				String LivingId =l.getId();
				String goodId =UIUtils.getSelectedValue(subType);
				gb=Services.getOtherBusiService().getGoodBorrow(LivingId, goodId);
			}else if(type.equals(InComeItemType.XCK.name())){
			    name =InComeItemType.valueOf(type).getName();
			    isConsume=false;
			}else
			{
				name =InComeItemType.valueOf(type).getName();
			    isConsume=true;
			}
			if(name.equals("")){
				if(UIUtils.confirm("没有选择消费名称要继续吗？")){
					
				}
				else
					return ;
				
			}
		    String iPrice =price.getValue();
		    if(!Utils.checkPrice(iPrice)){
				UIUtils.alert("请输入合法值，非负数,小数不能超过2位");
				return ;
			}
			TbAccounts ac =new TbAccounts();
			ac.setLiving(l);
			ac.setType(type);
			ac.setName(name);
			ac.setCounts(1);
			ac.setEndstate("否");
			if(isConsume)
			   ac.setPrice(-Utils.toPrice(iPrice));
			else
				ac.setPrice(Utils.toPrice(iPrice));
			ac.setPrice2(Utils.toPrice(iPrice));
			ac.setOpdate(DateUtil.currentDateToString("yyyy-MM-dd"));
			ac.setOptime(new Date());
			ac.setRemark(sRemark);
			ac.setOpUser(Hotel.getCurrentUser().getName());
			l=incomeService.inCome(ac, isConsume,gb);
			initTextbox(l);
			opTip.setValue("操作成功!");
			if(!manyTimesInChb.isChecked()){
				this.detach();
			}
		}catch(Exception e)
		{
			opTip.setValue("操作失败!"+e.getMessage());
			e.printStackTrace();
			
		}
		
	}
	public List<Living> getSelectLive() {
		return selectLive;
	}
	public void setSelectLive(List<Living> selectLive) {
		this.selectLive = selectLive;
	}
	

}
